package com.ktg.mes.chanjet.task;

import cn.hutool.json.JSONObject;
import com.google.gson.internal.LinkedTreeMap;
import com.ktg.common.exception.BussinessException;
import com.ktg.mes.chanjet.content.InventoryClassContent;
import com.ktg.mes.chanjet.entity.TpItem;
import com.ktg.mes.chanjet.response.InventoryClassResponse;
import com.ktg.mes.chanjet.service.TpItemService;
import com.ktg.mes.chanjet.spi.ChanjetSpi;
import com.ktg.mes.md.domain.MdItem;
import com.ktg.mes.md.service.IMdItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * T+ 存货同步
 */

@Slf4j
@Component("materielSyncDataTask")
public class MaterielSyncDataTask {

    @Autowired
    IMdItemService iMdItemService;
    @Autowired
    ChanjetSpi chanjetSpi;


    public void execute(){
        try{
            JSONObject js = new JSONObject();
            js.putOpt("SelectFields",
                            "id," +
                            "code," +
                            "name," +
                            "specification," +
                            "BaseUnitName," +
                            "BaseUnitCode," +
                            "InventoryClass.Id," +
                            "InventoryClass.Code," +
                            "InventoryClass.Name," +
                            "Disabled,InventoryDescript," +
                            "taxRate.name," +    //税率
                            "OssImageList," +
                            "Expired," +
                            "priuserdefnvc1," +   //粘度
                            "priuserdefnvc2," +   // 有无标签
                            "priuserdefnvc3," +   // 计量单位
                            "priuserdefnvc4," +    //当前命名
                            "priuserdefnvc5,"+    //  箱包装规格
                            "Shorthand," +   //助记码
                            "LowQuantity," +   // 最低库存
                            "SafeQuantity," +  // 安全库存
                            "TopQuantity"  // 最高库存
            );
            InventoryClassContent content = new InventoryClassContent();
            content.setParam(js);
            InventoryClassResponse materielResponse = chanjetSpi.getMaterielList(content);
            List<LinkedTreeMap> data = materielResponse.getData();
            List<MdItem>  mdItems = clearUpData(data);
            iMdItemService.saveOrUpdateBatch(mdItems);
        }catch (Exception ex){
            log.error("同步T+存货 信息出错",ex);
            throw new BussinessException("执行失败");
        }
    }
    private List<MdItem> clearUpData(List<LinkedTreeMap> data) {
        List<MdItem> list = new ArrayList<>();
        for (LinkedTreeMap map : data) {
            MdItem mdItem = new MdItem();
            mdItem.setItemId(new BigDecimal((Double) map.get("id")).longValue());
            mdItem.setItemCode((String) map.get("code"));
            mdItem.setItemName((String) map.get("name"));
            mdItem.setSpecification((String) map.get("specification"));
            mdItem.setUnitOfMeasure((String)map.get("BaseUnitName"));
            mdItem.setMeasureNo((String)map.get("BaseUnitCode"));
            mdItem.setEnableFlag(((String) map.get("Disabled")).equals("False") ? "Y" : "N");
            mdItem.setRemark((String) map.getOrDefault("InventoryDescript", ""));
            if (map.get("InventoryClass") != null) {
                LinkedTreeMap inventoryClass = (LinkedTreeMap) map.get("InventoryClass");
                mdItem.setItemTypeId(new BigDecimal((Double) inventoryClass.get("Id")).longValue());
                mdItem.setItemTypeCode((String)inventoryClass.get("Code"));
                mdItem.setItemTypeName((String)inventoryClass.get("Name"));
            }
            if (map.get("taxRate") != null) {
                LinkedTreeMap taxRate = (LinkedTreeMap) map.get("taxRate");
                mdItem.setTaxRate((String)taxRate.get("name"));
            }
            mdItem.setViscosity((String)map.get("priuserdefnvc1"));
            mdItem.setTag((String)map.get("priuserdefnvc2"));
            mdItem.setPackingType((String)map.get("priuserdefnvc3"));
            mdItem.setCurrencyName((String)map.get("priuserdefnvc4"));
            mdItem.setInvBoxStd((String)map.get("priuserdefnvc5"));
            mdItem.setInvAddCode((String)map.get("Shorthand"));
            mdItem.setItemOrProduct("PRODUCT");
            if(map.get("SafeQuantity") == null){
                mdItem.setSafeStockFlag("N");
                mdItem.setMinStock(0.0d);
                mdItem.setMaxStock(0.0d);
            }else{
                mdItem.setSafeStockFlag("Y");
                mdItem.setMinStock(new BigDecimal((String)map.get("LowQuantity")).doubleValue());
                mdItem.setMaxStock(new BigDecimal((String)map.get("TopQuantity")).doubleValue());
            }
            mdItem.setUpdateTime(new Date());
            list.add(mdItem);
        }
        return list;
    }

}
